package com.uzai.console.service.sop.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.entity.User;
import com.uzai.common.enums.*;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.msg.MsgDto;
import com.uzai.common.msg.NoticeMsgDto;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.preserveuser.PreserveUserIdDto;
import com.uzai.console.dto.preserveuser.PreserveUserQuery;
import com.uzai.console.dto.preserveuser.PreserveUserSendMsgDto;
import com.uzai.console.dto.preserveuser.PreserveUserUpdateStatusDto;
import com.uzai.console.entity.Device;
import com.uzai.console.entity.PreserveUser;
import com.uzai.console.mapper.PreserveUserMapper;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.rocketmq.NoticeMsgPushProducerService;
import com.uzai.console.service.sop.PreserveUserService;
import com.uzai.console.service.user.UserService;
import com.uzai.console.vo.preserveuser.PreserveUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * SOP任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class PreserveUserServiceImpl implements PreserveUserService {

    @Autowired
    private PreserveUserMapper preserveUserMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private NoticeMsgPushProducerService noticeMsgPushProducerService;

    /**
     * 查询加进群好友设置信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return PreserveUserVo
     */
    public Page<PreserveUserVo> findByPage(PreserveUserQuery preserveUserQuery){

        //新建返回到页面的LIST对象
        List<PreserveUserVo> preserveUserVoList = new ArrayList<>();

        //执行分页查询对象
        Page<PreserveUser> page = new Page<>();
        preserveUserQuery.convert(page);
        List<PreserveUser> preserveUserList = preserveUserMapper.findByList(page, preserveUserQuery);
        if(preserveUserList != null){
            for (PreserveUser preserveUser : preserveUserList){
                PreserveUserVo preserveUserVo = new PreserveUserVo();
                BeanUtils.copyProperties(preserveUser, preserveUserVo);

                Long userId = preserveUser.getUserId();
                Integer deviceType = preserveUser.getDeviceType();
                String deviceTypeStr = null;
                DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getById(deviceType);
                if(deviceTypeEnum != null){
                    deviceTypeStr = deviceTypeEnum.getKey();
                }
                //查询会员信息
                User userInfo = userService.findUserInfo(userId, deviceTypeStr, preserveUser.getMerId());
                if(userInfo != null){
                    preserveUserVo.setUserId(userId);
                    preserveUserVo.setUserWxid(userInfo.getBizUserId());
                    preserveUserVo.setUserName(userInfo.getNickName());
                    preserveUserVo.setUserRemark(userInfo.getRemark());
                    preserveUserVo.setDeviceUniqueId(userInfo.getDeviceUniqueId());

                    //查询机器人信息
                    if(userInfo.getDeviceUniqueId() != null ){
                        Device device = deviceService.findDeviceInfoByDeviceUniqueId(userInfo.getDeviceUniqueId(), deviceTypeStr, preserveUser.getMerId());
                        if(device != null){
                            preserveUserVo.setDeviceId(device.getDeviceId());
                            preserveUserVo.setDeviceName(device.getDeviceName());
                            preserveUserVo.setDeiviceRemark(device.getDeviceRemark());
                        }
                    }
                }

                preserveUserVoList.add(preserveUserVo);
            }
        }
        //返回到页面分页对象
        Page<PreserveUserVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(preserveUserVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询待维护会员
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param preserveUserIdDto
     * @return null
     */
    public PreserveUserVo findById(PreserveUserIdDto preserveUserIdDto){
        PreserveUserVo preserveUserVo = new PreserveUserVo();

        if(preserveUserIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        PreserveUser preserveUser = preserveUserMapper.selectById(preserveUserIdDto.getId());
        if(preserveUser != null){
            BeanUtils.copyProperties(preserveUser, preserveUserVo);

            Long userId = preserveUser.getUserId();
            Integer deviceType = preserveUser.getDeviceType();
            String deviceTypeStr = null;
            DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getById(deviceType);
            if(deviceTypeEnum != null){
                deviceTypeStr = deviceTypeEnum.getKey();
            }
            //查询会员信息
            User userInfo = userService.findUserInfo(userId, deviceTypeStr, preserveUser.getMerId());
            if(userInfo != null){
                preserveUserVo.setUserId(userId);
                preserveUserVo.setUserWxid(userInfo.getBizUserId());
                preserveUserVo.setUserName(userInfo.getNickName());
                preserveUserVo.setUserRemark(userInfo.getRemark());
                preserveUserVo.setDeviceUniqueId(userInfo.getDeviceUniqueId());

                //查询机器人信息
                if(userInfo.getDeviceUniqueId() != null ){
                    Device device = deviceService.findDeviceInfoByDeviceUniqueId(userInfo.getDeviceUniqueId(), deviceTypeStr, preserveUser.getMerId());
                    if(device != null){
                        preserveUserVo.setDeviceId(device.getDeviceId());
                        preserveUserVo.setDeviceName(device.getDeviceName());
                        preserveUserVo.setDeiviceRemark(device.getDeviceRemark());
                    }
                }
            }
        }

        return preserveUserVo;
    }

    /**
     * 删除待维护会员状态
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param preserveUserIdDto
     * @return null
     */
    public void delete(PreserveUserIdDto preserveUserIdDto){
        if(preserveUserIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        PreserveUser preserveUser = preserveUserMapper.selectById(preserveUserIdDto.getId());

        if(preserveUser == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        preserveUserMapper.deleteById(preserveUserIdDto.getId());
    }



    /**
     * 修改待维护会员状态
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void updateStatus(PreserveUserUpdateStatusDto preserveUserUpdateStatusDto){
        if(preserveUserUpdateStatusDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        if(preserveUserUpdateStatusDto.getStatus() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入状态");
        }

        PreserveUser preserveUser = preserveUserMapper.selectById(preserveUserUpdateStatusDto.getId());

        if(preserveUser == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }
        preserveUser.setStatus(preserveUserUpdateStatusDto.getStatus());
        preserveUser.setUpdateTime(DateUtil.getNowTime());
        preserveUserMapper.updateById(preserveUser);
    }

    /**
     * 待维护会员发送消息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void sendMsg(PreserveUserSendMsgDto preserveUserSendMsgDto){
        if(preserveUserSendMsgDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        if(StringUtils.isBlank(preserveUserSendMsgDto.getMsg())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入消息内容");
        }

        PreserveUser preserveUser = preserveUserMapper.selectById(preserveUserSendMsgDto.getId());

        if(preserveUser == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        Long userId = preserveUser.getUserId();
        Integer deviceType = preserveUser.getDeviceType();
        String deviceTypeStr = null;
        DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.getById(deviceType);
        if(deviceTypeEnum != null){
            deviceTypeStr = deviceTypeEnum.getKey();
        }
        //查询会员信息
        User userInfo = userService.findUserInfo(userId, deviceTypeStr, preserveUser.getMerId());
        if(userInfo == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该会员信息不存在");
        }

        Device device = deviceService.findDeviceInfoByDeviceUniqueId(userInfo.getDeviceUniqueId(), deviceTypeStr, preserveUser.getMerId());
        if(device == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该会员所在机器人不存在");
        }

        //调用API服务发送消息
        //发送MQ消息
        MsgDto<NoticeMsgDto> msgDto = new MsgDto<>();
        NoticeMsgDto  noticeMsgDto = new NoticeMsgDto();
        msgDto.setContent(noticeMsgDto);
        msgDto.setId(IdWorker.getId());
        msgDto.setDeviceType(device.getRecvmsgType());
        msgDto.setDeviceUniqueId(userInfo.getDeviceUniqueId());
        msgDto.setMerId(userInfo.getMerId());
        msgDto.setMsgType(MessageFuncEnum.SEND_CHAT_MESSAGE.getValue());
        noticeMsgDto.setContent(preserveUserSendMsgDto.getMsg());
        noticeMsgDto.setBizUserId(userInfo.getBizUserId());
        noticeMsgDto.setContentType(MessageTypeEnum.MT_TEXT.getValue());
        noticeMsgDto.setGroupId(null);
        noticeMsgDto.setNickName(userInfo.getNickName());
        noticeMsgDto.setMsgSource(1);
        noticeMsgDto.setDelay(0);
        noticeMsgDto.setMsgBizType(KefuSendMsgEnum.UNSOLICITEDMSG.getKey());
        noticeMsgPushProducerService.sendMessage(msgDto);
    }
}
